{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "90ab87c1-278b-48c9-a7b4-57473f051cd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# If you are using Colab for free, we highly recommend you activate the T4 GPU\n",
    "# hardware accelerator. Our models are designed to run with at least 16GB\n",
    "# of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed\n",
    "# to the ~13GB Colab gives automatically.\n",
    "# To activate T4:\n",
    "# 1. click on the \"Runtime\" tab\n",
    "# 2. click on \"Change runtime type\"\n",
    "# 3. select T4 GPU under \"Hardware Accelerator\"\n",
    "# NOTE: there is a weekly usage limit on using T4 for free"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "42caad5f-939b-4e42-a207-b79000c02179",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: llmware in /opt/homebrew/lib/python3.11/site-packages (0.2.15)\n",
      "Requirement already satisfied: boto3==1.24.53 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (1.24.53)\n",
      "Requirement already satisfied: huggingface-hub>=0.19.4 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.23.2)\n",
      "Requirement already satisfied: numpy>=1.23.2 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (1.26.4)\n",
      "Requirement already satisfied: openai>=1.0.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (1.30.3)\n",
      "Requirement already satisfied: pymongo>=4.7.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (4.7.2)\n",
      "Requirement already satisfied: tokenizers>=0.15.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.15.2)\n",
      "Requirement already satisfied: torch>=1.13.1 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (2.2.1)\n",
      "Requirement already satisfied: transformers>=4.36.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (4.39.1)\n",
      "Requirement already satisfied: Wikipedia-API==0.6.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.6.0)\n",
      "Requirement already satisfied: psycopg-binary==3.1.17 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (3.1.17)\n",
      "Requirement already satisfied: psycopg==3.1.17 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (3.1.17)\n",
      "Requirement already satisfied: pgvector==0.2.4 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.2.4)\n",
      "Requirement already satisfied: colorama==0.4.6 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.4.6)\n",
      "Requirement already satisfied: einops==0.7.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.7.0)\n",
      "Requirement already satisfied: librosa>=0.10.0 in /opt/homebrew/lib/python3.11/site-packages (from llmware) (0.10.2.post1)\n",
      "Requirement already satisfied: botocore<1.28.0,>=1.27.53 in /opt/homebrew/lib/python3.11/site-packages (from boto3==1.24.53->llmware) (1.27.96)\n",
      "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /opt/homebrew/lib/python3.11/site-packages (from boto3==1.24.53->llmware) (1.0.1)\n",
      "Requirement already satisfied: s3transfer<0.7.0,>=0.6.0 in /opt/homebrew/lib/python3.11/site-packages (from boto3==1.24.53->llmware) (0.6.2)\n",
      "Requirement already satisfied: typing-extensions>=4.1 in /opt/homebrew/lib/python3.11/site-packages (from psycopg==3.1.17->llmware) (4.10.0)\n",
      "Requirement already satisfied: requests in /opt/homebrew/lib/python3.11/site-packages (from Wikipedia-API==0.6.0->llmware) (2.32.2)\n",
      "Requirement already satisfied: filelock in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.19.4->llmware) (3.14.0)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.19.4->llmware) (2024.3.1)\n",
      "Requirement already satisfied: packaging>=20.9 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.19.4->llmware) (24.0)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.19.4->llmware) (6.0.1)\n",
      "Requirement already satisfied: tqdm>=4.42.1 in /opt/homebrew/lib/python3.11/site-packages (from huggingface-hub>=0.19.4->llmware) (4.66.2)\n",
      "Requirement already satisfied: audioread>=2.1.9 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (3.0.1)\n",
      "Requirement already satisfied: scipy>=1.2.0 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (1.12.0)\n",
      "Requirement already satisfied: scikit-learn>=0.20.0 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (1.5.0)\n",
      "Requirement already satisfied: joblib>=0.14 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (1.4.2)\n",
      "Requirement already satisfied: decorator>=4.3.0 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (5.1.1)\n",
      "Requirement already satisfied: numba>=0.51.0 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (0.59.1)\n",
      "Requirement already satisfied: soundfile>=0.12.1 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (0.12.1)\n",
      "Requirement already satisfied: pooch>=1.1 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (1.8.1)\n",
      "Requirement already satisfied: soxr>=0.3.2 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (0.3.7)\n",
      "Requirement already satisfied: lazy-loader>=0.1 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (0.4)\n",
      "Requirement already satisfied: msgpack>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from librosa>=0.10.0->llmware) (1.0.8)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /opt/homebrew/lib/python3.11/site-packages (from openai>=1.0.0->llmware) (4.4.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /opt/homebrew/lib/python3.11/site-packages (from openai>=1.0.0->llmware) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /opt/homebrew/lib/python3.11/site-packages (from openai>=1.0.0->llmware) (0.27.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /opt/homebrew/lib/python3.11/site-packages (from openai>=1.0.0->llmware) (2.7.1)\n",
      "Requirement already satisfied: sniffio in /opt/homebrew/lib/python3.11/site-packages (from openai>=1.0.0->llmware) (1.3.1)\n",
      "Requirement already satisfied: dnspython<3.0.0,>=1.16.0 in /opt/homebrew/lib/python3.11/site-packages (from pymongo>=4.7.0->llmware) (2.6.1)\n",
      "Requirement already satisfied: sympy in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.13.1->llmware) (1.12)\n",
      "Requirement already satisfied: networkx in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.13.1->llmware) (3.3)\n",
      "Requirement already satisfied: jinja2 in /opt/homebrew/lib/python3.11/site-packages (from torch>=1.13.1->llmware) (3.1.4)\n",
      "Requirement already satisfied: regex!=2019.12.17 in /opt/homebrew/lib/python3.11/site-packages (from transformers>=4.36.0->llmware) (2024.5.15)\n",
      "Requirement already satisfied: safetensors>=0.4.1 in /opt/homebrew/lib/python3.11/site-packages (from transformers>=4.36.0->llmware) (0.4.3)\n",
      "Requirement already satisfied: idna>=2.8 in /opt/homebrew/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai>=1.0.0->llmware) (3.7)\n",
      "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /opt/homebrew/lib/python3.11/site-packages (from botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (2.9.0.post0)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.25.4 in /opt/homebrew/lib/python3.11/site-packages (from botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (1.26.18)\n",
      "Requirement already satisfied: certifi in /opt/homebrew/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.0.0->llmware) (2024.2.2)\n",
      "Requirement already satisfied: httpcore==1.* in /opt/homebrew/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai>=1.0.0->llmware) (1.0.5)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /opt/homebrew/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai>=1.0.0->llmware) (0.14.0)\n",
      "Requirement already satisfied: llvmlite<0.43,>=0.42.0dev0 in /opt/homebrew/lib/python3.11/site-packages (from numba>=0.51.0->librosa>=0.10.0->llmware) (0.42.0)\n",
      "Requirement already satisfied: platformdirs>=2.5.0 in /opt/homebrew/lib/python3.11/site-packages (from pooch>=1.1->librosa>=0.10.0->llmware) (4.2.2)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /opt/homebrew/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->openai>=1.0.0->llmware) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.18.2 in /opt/homebrew/lib/python3.11/site-packages (from pydantic<3,>=1.9.0->openai>=1.0.0->llmware) (2.18.2)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests->Wikipedia-API==0.6.0->llmware) (3.3.2)\n",
      "Requirement already satisfied: threadpoolctl>=3.1.0 in /opt/homebrew/lib/python3.11/site-packages (from scikit-learn>=0.20.0->librosa>=0.10.0->llmware) (3.4.0)\n",
      "Requirement already satisfied: cffi>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from soundfile>=0.12.1->librosa>=0.10.0->llmware) (1.16.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /opt/homebrew/lib/python3.11/site-packages (from jinja2->torch>=1.13.1->llmware) (2.1.5)\n",
      "Requirement already satisfied: mpmath>=0.19 in /opt/homebrew/lib/python3.11/site-packages (from sympy->torch>=1.13.1->llmware) (1.3.0)\n",
      "Requirement already satisfied: pycparser in /opt/homebrew/lib/python3.11/site-packages (from cffi>=1.0->soundfile>=0.12.1->librosa>=0.10.0->llmware) (2.22)\n",
      "Requirement already satisfied: six>=1.5 in /opt/homebrew/lib/python3.11/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.28.0,>=1.27.53->boto3==1.24.53->llmware) (1.16.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install llmware"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "809f3d5a-37f9-4843-b3dd-080a10e9d8c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llmware.models import ModelCatalog\n",
    "from llmware.parsers import WikiParser\n",
    "\n",
    "\n",
    "#   our input - financial news article - note: the 'company founding date' is not mentioned in the text\n",
    "#   -- our worry is that the model will either 'make up' an answer from background knowledge (which may or may not\n",
    "#      be accurate), or try to incorrectly use a date in the text, e.g, February 29, 2024.\n",
    "\n",
    "\n",
    "text =(\"BEAVERTON, Ore.--(BUSINESS WIRE)--NIKE, Inc. (NYSE:NKE) today reported fiscal 2024 financial results for its \"\n",
    "      \"third quarter ended February 29, 2024.) “We are making the necessary adjustments to drive NIKE’s next chapter \"\n",
    "      \"of growth Post this Third quarter revenues were slightly up on both a reported and currency-neutral basis* \"\n",
    "      \"at $12.4 billion NIKE Direct revenues were $5.4 billion, slightly up on a reported and currency-neutral basis \"\n",
    "      \"NIKE Brand Digital sales decreased 3 percent on a reported basis and 4 percent on a currency-neutral basis \"\n",
    "      \"Wholesale revenues were $6.6 billion, up 3 percent on a reported and currency-neutral basis Gross margin \"\n",
    "      \"increased 150 basis points to 44.8 percent, including a detriment of 50 basis points due to restructuring charges \"\n",
    "      \"Selling and administrative expense increased 7 percent to $4.2 billion, including $340 million of restructuring \"\n",
    "      \"charges Diluted earnings per share was $0.77, including $0.21 of restructuring charges. Excluding these \"\n",
    "      \"charges, Diluted earnings per share would have been $0.98* “We are making the necessary adjustments to \"\n",
    "      \"drive NIKE’s next chapter of growth,” said John Donahoe, President & CEO, NIKE, Inc. “We’re encouraged by \"\n",
    "      \"the progress we’ve seen, as we build a multiyear cycle of new innovation, sharpen our brand storytelling and \"\n",
    "      \"work with our wholesale partners to elevate and grow the marketplace.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b6c5f0ab-e5d7-41da-8487-15cd1647962e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def not_found_then_triage_lookup():\n",
    "\n",
    "    print(\"\\nNot Found Example - if info not found, then lookup in another source.\\n\")\n",
    "\n",
    "    extract_key = \"company founding date\"\n",
    "    dict_key = extract_key.replace(\" \", \"_\")\n",
    "\n",
    "    company_founding_date = \"\"\n",
    "\n",
    "    # step 1 - run an extract function call on the text\n",
    "    model = ModelCatalog().load_model(\"slim-extract-tool\", temperature=0.0, sample=False)\n",
    "    response = model.function_call(text, function=\"extract\", params=[extract_key])\n",
    "    llm_response = response[\"llm_response\"]\n",
    "\n",
    "    print(f\"update: first text reviewed for {extract_key} - llm response: \", llm_response)\n",
    "\n",
    "    # unpack the output\n",
    "    if dict_key in llm_response:\n",
    "\n",
    "        company_founding_date = llm_response[dict_key]\n",
    "\n",
    "        if len(company_founding_date) > 0:\n",
    "\n",
    "            # in this case, the value is a list with at least one element, so an 'answer' was found\n",
    "            company_founding_date = company_founding_date[0]\n",
    "            print(f\"update: found the {extract_key} value - \", company_founding_date)\n",
    "            return company_founding_date\n",
    "\n",
    "        else:\n",
    "\n",
    "            # step 2 - could not find the answer in the original source materials\n",
    "            #   e.g., the len of the list associated with the key is zero, or []\n",
    "\n",
    "            print(f\"update: did not find the target value in the text - {company_founding_date}\")\n",
    "            print(\"update: initiating a secondary process to try to find the information\")\n",
    "\n",
    "            #   look up the company name from the original text\n",
    "            response = model.function_call(text, function=\"extract\", params=[\"company name\"])\n",
    "\n",
    "            if \"company_name\" in response[\"llm_response\"]:\n",
    "                company_name = response[\"llm_response\"][\"company_name\"][0]\n",
    "\n",
    "                if company_name:\n",
    "                    print(f\"\\nupdate: found the company name - {company_name} - now using to lookup in secondary source\")\n",
    "\n",
    "                    # use the company name to lookup materials in Wikipedia secondary source\n",
    "                    output = WikiParser().add_wiki_topic(company_name,target_results=1)\n",
    "\n",
    "                    if output:\n",
    "\n",
    "                        supplemental_text = output[\"articles\"][0][\"summary\"]\n",
    "\n",
    "                        if len(supplemental_text) > 150:\n",
    "                            supplemental_text_pp = supplemental_text[0:150] + \" ... \"\n",
    "                        else:\n",
    "                            supplemental_text_pp = supplemental_text\n",
    "\n",
    "                        print(f\"update: using lookup - {company_name} - found secondary source article \"\n",
    "                              f\"(extract displayed) - \", supplemental_text_pp)\n",
    "\n",
    "                        #   finally, try to get the company founding date from the supplemental text\n",
    "                        new_response = model.function_call(supplemental_text,params=[\"company founding date\"])\n",
    "\n",
    "                        print(\"\\nupdate: reviewed second source article - \", new_response[\"llm_response\"])\n",
    "\n",
    "                        if \"company_founding_date\" in new_response[\"llm_response\"]:\n",
    "                            company_founding_date = new_response[\"llm_response\"][\"company_founding_date\"]\n",
    "                            if company_founding_date:\n",
    "                                print(\"update: success - found the answer - \", company_founding_date)\n",
    "\n",
    "    return company_founding_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "296a32b1-db9f-4e09-9c6f-ab4c40046634",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Not Found Example - if info not found, then lookup in another source.\n",
      "\n",
      "update: first text reviewed for company founding date - llm response:  {'company_founding_date': []}\n",
      "update: did not find the target value in the text - []\n",
      "update: initiating a secondary process to try to find the information\n",
      "\n",
      "update: found the company name - NIKE, Inc. - now using to lookup in secondary source\n",
      "update: using lookup - NIKE, Inc. - found secondary source article (extract displayed) -  Nike, Inc. (stylized as NIKE) is an American athletic footwear and apparel corporation headquartered near Beaverton, Oregon, United States. It is the  ... \n",
      "\n",
      "update: reviewed second source article -  {'company_founding_date': ['January 25, 1964']}\n",
      "update: success - found the answer -  ['January 25, 1964']\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "\n",
    "    founding_date = not_found_then_triage_lookup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddd92bbb-1f24-4684-b0ca-61d0e0a26ff9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
